package cn.wsh.org.service.impl;

import cn.wsh.basic.exception.BusinessException;
import cn.wsh.basic.util.BaiduAuditUtils;
import cn.wsh.org.domain.Employee;
import cn.wsh.org.domain.Shop;
import cn.wsh.org.domain.ShopAuditLog;
import cn.wsh.org.mapper.EmployeeMapper;
import cn.wsh.org.mapper.ShopAuditLogMapper;
import cn.wsh.org.mapper.ShopMapper;
import cn.wsh.org.service.IShopService;
import cn.wsh.basic.service.impl.BaseServiceImpl;
import cn.wsh.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wushenghua
 * @since 2022-02-11
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一：参数校验
        //1.空校验 密码要一致
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("参数不能为空！！");
        }
        //2.非法校验（百度AI智能审核） 名称、图片
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("敏感字，请换名称！！");
        }
        //二：业务校验
        //1.店铺不能被注册
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if (shopTmp != null) {
            throw new BusinessException("店铺已存在，请直接登录！！如果忘记密码，请走找回密码流程！！");
        }
        //2.employee是否曾今注册过了
        Employee admin = shop.getAdmin();
        Employee empTmp = employeeMapper.loadByUsename(admin.getUsername());
        if (empTmp != null && empTmp.getShop_id() != null) {
            throw new BusinessException("管理员已不能管理多个店铺");
        }
        if (empTmp == null) {
            //新增员工
            //2.存储employee
            employeeMapper.save(admin);//返回自增ID
            empTmp = admin;
        }
        //不管是新增和还是从数据库查询出原来的，到这里，都有员工了
        //1.存储shop
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);//返回自增ID
        //  很容易忘记 需要重新修改employee中的shopId
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);
    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态为待激活
        //System.out.println(shopAuditLog.getShopId());
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        //System.out.println(shop);
        shop.setState(1);
        shopMapper.update(shop);
        //2.记录审核日志
        //设置当前登录人，由于我们现在没有，直接写死，后面来改
        shopAuditLog.setAuditId(334L);
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送激活邮件 + 或短信  我这是瓦的，你们自己企业里面玩真实的
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2645042904@qq.com");
        message.setTo("2645042904@qq.com");
        message.setSubject("审核通过");
        message.setText("恭喜！！");
        javaMailSender.send(message);

    }


    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态为驳回
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        //System.out.println(shop);
        shop.setState(-1);
        shopMapper.update(shop);
        //2.记录审核日志
        //设置当前登录人，由于我们现在没有，直接写死，后面来改
        shopAuditLog.setAuditId(334L);
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送驳回邮件 + 或短信  我这是瓦的，你们自己企业里面玩真实的
        // 在邮件中，提供直接修改店铺信息的链接，方便客人直接修改已有的店铺信息 Register2.vue
        //3.1 查找出店长的邮箱
        System.out.println("11111111111111111111111111111111111");
        //Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        System.out.println("222222222222222222222222222222222");
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        System.out.println("33333333333333333333333333333333333333333");
        //3.2 给店长发邮件
        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //复杂邮件对象
        //创建复杂邮件对象
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2645042904@qq.com");
            helper.setSubject("邮件标题");
            //第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>啊哈？</h1>"+
                    "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>",true);

            //添加附件
            helper.addAttachment("文件名.jpg",new File("D:\\图片库\\14.jpg"));
            //收件人
            helper.setTo("2645042904@qq.com");
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }



    }

    @Override
    public List<ShopVo> countByState() {
        return shopMapper.queryStateCount();
    }


}
